<template>
    <div>
        <div class="page-title">编辑指南</div>
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="指南标题.">
                <el-input ref="title" v-model="form.title" placeholder="请输入指南标题"></el-input>
            </el-form-item>

            <el-form-item label="所属分类.">
                <el-select ref="type" v-model="form.type" placeholder="-- 请选择分类 --">
                    <el-option v-for="i in options" :key="i" :label="i.name" :value="i.id"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="附件上传.">
                <el-upload :action="uploadFile" :data="fileData" :before-upload="beforeUpload"
                    :on-success="uploadSuccess" :show-file-list="false">
                    <i v-if="form.file == ''" class="el-icon-plus"></i>
                    <div v-else>
                        <i class="i i-xls"></i>
                        <el-button class="reupload-file" icon="el-icon-refresh" size="mini">重新上传</el-button>
                    </div>
                    <template #tip>
                        <div class="el-upload__tip">
                            允许上传 ZIP / RAR 格式的压缩包，文件大小不超过 2MB
                        </div>
                    </template>
                </el-upload>
            </el-form-item>

            <el-form-item label="指南排序.">
                <el-input ref="sort" v-model="form.sort" placeholder="请输入指南排序，（ 非必填，默认为0 ）"></el-input>
            </el-form-item>

            <el-form-item label="指南状态.">
                <el-switch v-model="form.status" active-color="#f8c15c"></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="success" icon="el-icon-check" @click="submit">修改</el-button>
                <el-button icon="el-icon-close" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {
        get,
        put
    } from '@/network/request'
    import {
        __URL__
    } from '@/main'

    export default {
        name: 'EditGuide',
        data() {
            return {
                form: {
                    id: this.$route.params.id,
                    cid: sessionStorage.getItem('cid'),
                    title: '',
                    type: '',
                    file: '',
                    sort: '',
                    status: false
                },
                options: [], // 分类数组
                uploadFile: __URL__ + '/uploadFile', // 上传文件地址
                fileData: {
                    ext: '' // 文件后缀
                }
            }
        },
        mounted() {
            this.$nextTick(() => {
                // 获取信息
                get('/getGuide', {
                    id: this.$route.params.id,
                    cid: sessionStorage.getItem('cid')
                }).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.form.title = result.data.title
                        this.form.sort = result.data.sort
                        this.form.file = result.data.file
                        this.form.type = (result.data.tid != 0) ? result.data.tid : ''
                        this.form.status = (result.data.status == 1) ? true : false
                    } else {
                        this.$router.push('/404')
                    }
                }).catch()

                // 获取分类
                get('/selectGuideType', {
                    cid: sessionStorage.getItem('cid')
                }).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.options = result.data
                    }
                }).catch()
            })
        },
        methods: {
            // 上传准备
            beforeUpload(file) {
                if (['application/zip', 'application/rar', 'application/x-zip-compressed'].indexOf(file.type) == -1) {
                    this.$message.error({
                        message: '请上传正确的文件格式！',
                        offset: 70
                    })
                    return false
                }

                if (file.type == 'application/zip' || file.type == 'application/x-zip-compressed') {
                    this.fileData.ext = 'zip';
                }

                if (file.type == 'application/rar') {
                    this.fileData.ext = 'rar';
                }

                const size = file.size / 1024 / 1024 < 2
                if (!size) {
                    this.$message.error({
                        message: '文件大小不能超过 2MB！',
                        offset: 70
                    })
                    return false
                }
            },
            // 上传成功
            uploadSuccess(res) {
                this.form.file = res.data
                this.$notify.success({
                    title: '系统提示',
                    message: '文件上传成功',
                    offset: 70
                })
            },
            // 提交
            submit() {
                if (this.form.title == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入指南标题',
                        offset: 70
                    })
                    this.$refs['title'].focus()
                    return false
                }

                if (this.form.type == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请选择分类',
                        offset: 70
                    })
                    this.$refs['type'].focus()
                    return false
                }

                put('/modifyGuide', this.form).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.$notify.success({
                            title: '系统提示',
                            message: '指南修改成功',
                            offset: 70
                        })
                        this.$router.push('/guide')
                    }
                }).catch()
            },
            // 取消
            cancel() {
                this.$router.push('/guide')
            }
        }
    }
</script>

<style>
</style>